﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApp1.类
{
    public  class 禾川PLCQ0
    {
        HslCommunication.ModBus.ModbusTcpNet Modbus = new HslCommunication.ModBus.ModbusTcpNet();
        public Boolean IsConnect = false;
        public Boolean ConnectSrver(string IP,string port)
        {
            try
            {
                Modbus.IpAddress = IP;
                Modbus.Port = Convert.ToInt32(port);
                Modbus.Station = 1;
                if (Modbus.ConnectServer().IsSuccess)
                {
                    IsConnect = true;
                    return true;
                }
                else
                {
                    IsConnect = false;
                    return false;
                }
            }
            catch (Exception ex)
            {

                IsConnect = false;
                return false;
            }
                 
        }
        public Boolean ConnectSrver()
        {
            try
            {
                if (Modbus.ConnectServer().IsSuccess)
                {
                    IsConnect = true;
                    return true;
                }
                else
                {
                    IsConnect = false;
                    return false;
                }
            }
            catch (Exception ex)
            {

                IsConnect = false;
                return false;
            }

        }
        public void DisConnect()
        {
            if(IsConnect)
            {
                Modbus.ConnectClose();
                IsConnect = false;
            }
        }

        public void Init(string IP, int port, byte station)
        {
            Modbus.IpAddress = IP;
            Modbus.Port = port;
            Modbus.Station = station;
        }

        public Boolean WritePLC_D(string address,string value)
        {
            try
            {
                ushort valuet = Convert.ToUInt16(value);
                if(IsConnect)
                {
                    if (Modbus.Write(address, valuet).IsSuccess)
                    {
                        return true;
                    }else
                    {
                        return false;
                    }

                }else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public string ReadPLC_D(string address, string num)
        {
            string result = "-1";
            try
            {
                ushort valuet = Convert.ToUInt16(num);
                if (IsConnect)
                {
                    HslCommunication.OperateResult<UInt16> t = Modbus.ReadUInt16(address);
                    if(t.IsSuccess)
                    {
                        result = t.Content.ToString();
                        return result;
                    }else
                    {
                        return result;
                    }
                    
                }
                else
                {
                    return result;
                }
            }
            catch (Exception ex)
            {
                return result;
            }
        }

        public Boolean WritePLC_M(string address,Boolean value)
        {
            try
            {
           
                if(IsConnect)
                {
                    if (Modbus.WriteCoil(address, value).IsSuccess)
                    {
                        return true;
                    }else
                    {
                        return false;
                    }

                }else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public string ReadPLC_M(string address)
        {
            string result="-1";
            try
            {               
                if (IsConnect)
                {
                    HslCommunication.OperateResult<bool> t = Modbus.ReadCoil(address);
                    if (t.IsSuccess)
                    {
                        result = t.Content.ToString();
                        return result;
                    }
                    else
                    {
                        return result;
                    }

                }
                else
                {
                    return result;
                }
            }
            catch (Exception ex)
            {
                return result;
            }
        }

    }
}
